package com.yj.event_listener.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.Telephony;
import android.util.Log;

import androidx.annotation.RequiresApi;

public class PhoneUtil {


    public static Cursor getFirstMissedCall(Context context) {
        // Define the content URI for SMS messages
        Uri uri = CallLog.Calls.CONTENT_URI;

        // Define the projection (columns to retrieve)
        String[] projection = {
                CallLog.Calls.CACHED_NAME, // 通话记录的联系人
                CallLog.Calls.NUMBER, // 通话记录的电话号码
                CallLog.Calls.TYPE // 通话类型
        };



        // Perform the query
        ContentResolver contentResolver = context.getContentResolver();
        Bundle bundle = new Bundle();
        bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, 1);
        bundle.putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, new String[]{CallLog.Calls.DATE});
        bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
        Cursor cursor = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            cursor = contentResolver.query(uri, projection, bundle, null);
        }

        return cursor;
    }
}
